﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.Core.IService;
using Empires.CRL.DataContract;
using Empires.RAL.Access;
using Empires.RAL.Pump;
using Empires.BLL.ServerProvider;

namespace Empires.BLL.Core.Implement
{
    public class CompanyProductService : ICompanyProductService
    {
        public List<DefaultDataPack> GetProductWithList(string copName, string type, string subject, int page, int size)
        {
            ICompanyProductAccess service = new CompanyProductDataPump();
            int itemCount = GetProductItemCount(copName, type, subject);
            return CompanyHelper.InjuectPagingInfo((currentPage) =>
            {
                return service.GetProductWithList(copName, type, subject, currentPage, size);
            },
            itemCount, page, size);
            //int pagecount = itemCount % size == 0 ? itemCount / size : itemCount / size + 1;
            //page = page < 0 ? 0 : page;
            //page = page > pagecount - 1 ? pagecount - 1 : page;
            //int previouspage = page == 0 ? 0 : page - 1;
            //int nextpage = page == pagecount - 1 ? pagecount - 1 : page + 1;
            //var list = service.GetProductWithList(copName, type, subject, page, size);
            
            //return list.Select(c =>
            //{
            //    c.ITEMCOUNT = itemCount;
            //    c.PAGESIZE = size;
            //    c.CURRENTPAGE = page + 1;
            //    c.PAGECOUNT = pagecount;
            //    c.PREVIOUSPAGE = previouspage;
            //    c.NEXTPAGE = nextpage;
            //    c.FIRSTPAGE = 0;
            //    c.LASTPAGE = pagecount - 1;
            //    return c;
            //}).ToList();
        }

        public int GetProductItemCount(string copName, string type, string subject)
        {
            ICompanyProductAccess service = new CompanyProductDataPump();
            return service.GetProductItemCount(copName, type, subject);
        }

        public List<DefaultDataPack> GetProductDetail(int pid)
        {
            ICompanyProductAccess service = new CompanyProductDataPump();
            return service.GetProductDetail(pid);
        }

        public bool ProductEdit(string id, ProductDataPack datapack)
        {
            if (!String.IsNullOrEmpty(datapack.URL))
            {
                var result = ServerProvider.Tools.UploadFiles(
                    new List<UploadFileDataPack>() { 
                        new UploadFileDataPack(){
                            BasePath = String.Format(Empires.CRL.Tools.Utils.ReadAppSetting("CustomerImage"),id),
                            TypeFilter= new List<FileType>(){ FileType.jpg,FileType.png },
                            UploadName="URL",
                            CopyMode= CopyMode.rename
                        }
                    }, System.Web.HttpContext.Current.Request.Files);
                string url = string.Empty;
                if (result.TryGetValue("URL", out url))
                {
                    datapack.URL = String.Format("<img src=\"{0}\" />", url);
                }
                else
                {
                    datapack.URL = String.Empty;
                }
            }
            ICompanyProductAccess service = new CompanyProductDataPump();
            return service.ProductEdit(id, datapack);
        }


        public bool ProductDelete(string copname, int pid)
        {
            ICompanyProductAccess service = new CompanyProductDataPump();
            return service.ProductDelete(copname, pid);
        }


        public bool ProductAdd(string copname, ProductDataPack productDataPack)
        {
            if (!String.IsNullOrEmpty(productDataPack.URL))
            {
                var result = ServerProvider.Tools.UploadFiles(
                    new List<UploadFileDataPack>() { 
                        new UploadFileDataPack(){
                            BasePath = String.Format(Empires.CRL.Tools.Utils.ReadAppSetting("CustomerImage"),copname),
                            TypeFilter= new List<FileType>(){ FileType.jpg,FileType.png },
                            UploadName="URL",
                            CopyMode= CopyMode.rename
                        }
                    }, System.Web.HttpContext.Current.Request.Files);
                string url = string.Empty;
                if (result.TryGetValue("URL", out url))
                {
                    productDataPack.URL = String.Format("<img src=\"{0}\" />", url);
                }
                else
                {
                    productDataPack.URL = String.Empty;
                }
            }
            ICompanyProductAccess service = new CompanyProductDataPump();
            return service.ProductAdd(copname, productDataPack);
        }
    }
}
